#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <malloc.h>
#include <string.h>

template<class T>
void createDoS(T* h_U,int M,long hits,char* fileName)
{
  int e,j;
  long RN = hits;
  double PI=3.1415926;

  T *Re = (T*)malloc(sizeof(T) * RN);
  memset(Re,0,sizeof(T) * RN);
  T t0,t1,t2;
  for(e=0;e<RN;e+=1)
  {
    T Temp=0;
    T x=(T) (e-RN/2) / (RN+1) * 2;
    for(j=0;j<M;j++)
    {
      if(j==0)
      {
        t0 = 1;
        Temp = t0;
      }
      else if(j==1)
      {
        t1=x;
        Temp = t1;
      }
      else
      {
        t2 = 2 * x * t1 - t0;
        Temp=t2;
        t0=t1;
        t1=t2;
      }
      T kernel =
             (T)1/(M + 1)*((M-j+1)*cos(PI*j/(M+1))
                           + sin(PI*j/(M+1))*cos(PI/(M+1))/sin(PI/(M+1)));
      if(j==0)
      	Re[e] += h_U[0]*kernel;
      else
      	Re[e] +=2 * h_U[j] * Temp * kernel;
    }
    Re[e] *= 1/(3.14*sqrt(1-x*x));
  }
  FILE* file = fopen(fileName,"wb");
  fwrite(Re,sizeof(T),RN,file);
  fclose(file);
}
